import os
from flask import Blueprint, request, jsonify, render_template

from applications.common.utils.http import fail_api

dir_path = os.path.dirname(os.path.realpath(__file__)).replace("\\", "/")

bp = Blueprint('dev_plugin', __name__,
               template_folder='templates',
               static_folder='static',
               static_url_path='/dev/static',
               url_prefix='/dev/admin/plugin')

@bp.route('/')
def index():
    return render_template('admin/plugin/index.html')

@bp.route('/add')
def add():
    ##post
    if request.method == 'POST':
        data = request.form
        if data['name'] == '' or data['version'] == '' or data['folder'] == '':
            return fail_api('请填写完整信息')
        folder = data['folder']
        if os.path.isdir('plugins/' + folder) or folder in ['admin', 'plugins', 'system','dev']:
            return fail_api('文件夹存在或是不能使用的！')
        #创建文件夹
        os.mkdir('plugins/' + folder)
        os.makedirs('plugins/' + folder + '/static')
        os.makedirs('plugins/' + folder + '/templates')
        os.makedirs('plugins/' + folder + '/templates/admin')
        os.makedirs('plugins/' + folder + '/templates/home')
    else:
        return render_template('admin/plugin/add.html')